package com.gklee.regionselector;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.flexbox.FlexboxLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RegionSelectDialog
{

    private final Activity mContext;
    private final Dialog dialog;
    private final TextView tv_province;
    private final ImageView iv_back;
    private final TextView tv_city;
    private final TextView tv_zone_dialog;
    private final TextView tv_area;
    private final ListView lv_province;
    private final ListView lv_city;
    private final ListView lv_zone;
    private final ListView lv_area;
    private List<SelectBean> proince = new ArrayList<>();
    private QuickAdapter<SelectBean> provinceAdapter;
    private final QuickAdapter cityAdapter;
    private final QuickAdapter zoneAdapter;
    private final QuickAdapter areaAdapter;
    private OnRegionDataSetListener mProvinDataListenr;
    private List<SelectBean> cityList;
    private List<SelectBean> zoneList;
    private List<SelectBean> areaList;
    private final RegionLevel regionLevel;
    private final boolean isFour;
    private boolean isMultiSelect = true;

    private HashMap<Integer, SelectBean> checkMap = new HashMap<>();

    private FlexboxLayout flex;

    public RegionSelectDialog(Activity context, RegionLevel levelFour)
    {
        mContext = context;

        dialog = new Dialog(mContext, R.style.regionsDialog);
        dialog.setContentView(R.layout.dialog_address_b);
        dialog.setCanceledOnTouchOutside(true);
        dialog.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (mProvinDataListenr != null)
                {
                    mProvinDataListenr.resultSelectedList(getSelectList());
                    dialog.dismiss();
                }
            }
        });

        flex = dialog.findViewById(R.id.flexbox);

        tv_province = (TextView) dialog.findViewById(R.id.tv_province);
        iv_back = (ImageView) dialog.findViewById(R.id.iv_back);
        tv_city = (TextView) dialog.findViewById(R.id.tv_city);
        tv_zone_dialog = (TextView) dialog.findViewById(R.id.tv_zone_dialog);
        tv_area = dialog.findViewById(R.id.tv_area_dialog);

        lv_province = (ListView) dialog.findViewById(R.id.lv_address);
        lv_city = (ListView) dialog.findViewById(R.id.lv_city);
        lv_zone = (ListView) dialog.findViewById(R.id.lv_zone);
        lv_area = dialog.findViewById(R.id.lv_area);
        regionLevel = levelFour;
        if (regionLevel.equals(RegionLevel.LEVEL_FOUR))
        {
            isFour = true;
            tv_area.setVisibility(View.INVISIBLE);
            lv_area.setVisibility(View.INVISIBLE);
        } else
        {
            isFour = false;
            tv_area.setVisibility(View.GONE);
            lv_area.setVisibility(View.GONE);
        }
        provinceAdapter = new QuickAdapter<SelectBean>(mContext, R.layout.item_address_dialog)
        {
            @Override
            protected void convert(BaseAdapterHelper helper, SelectBean item)
            {
                helper.setText(R.id.tv_item, item.getName());
            }
        };
        cityAdapter = new QuickAdapter<SelectBean>(mContext, R.layout.item_address_dialog)
        {
            @Override
            protected void convert(BaseAdapterHelper helper, SelectBean item)
            {
                helper.setText(R.id.tv_item, item.getName());
            }
        };
        zoneAdapter = new QuickAdapter<SelectBean>(mContext, R.layout.item_address_dialog)
        {
            @Override
            protected void convert(BaseAdapterHelper helper, SelectBean item)
            {
                helper.setText(R.id.tv_item, item.getName());
            }
        };
        areaAdapter = new QuickAdapter<SelectBean>(mContext, R.layout.item_checkbox_dialog)
        {
            @Override
            protected void convert(BaseAdapterHelper helper, SelectBean item)
            {
                helper.setText(R.id.tv_item, item.getName());
                View view = helper.getView();
                CheckBox box = view.findViewById(R.id.check);
                int id = item.getId();
                if(checkMap.containsKey(id))
                {
                    box.setChecked(true);
                }
                else
                {
                    box.setChecked(false);
                }
            }
        };
        lv_province.setAdapter(provinceAdapter);
        lv_city.setAdapter(cityAdapter);
        lv_zone.setAdapter(zoneAdapter);
        lv_area.setAdapter(areaAdapter);

        tv_province.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                lv_province.setVisibility(View.VISIBLE);
                lv_zone.setVisibility(View.GONE);
                lv_city.setVisibility(View.GONE);
                lv_area.setVisibility(View.GONE);
                tv_province.setTextColor(mContext.getResources().getColor(R.color.theme_red));
                tv_city.setTextColor(mContext.getResources().getColor(R.color.region_black));
                tv_zone_dialog.setTextColor(mContext.getResources().getColor(R.color.region_black));
                tv_area.setTextColor(mContext.getResources().getColor(R.color.region_black));
            }
        });
        tv_city.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                lv_province.setVisibility(View.GONE);
                lv_zone.setVisibility(View.GONE);
                lv_city.setVisibility(View.VISIBLE);
                lv_area.setVisibility(View.GONE);
                tv_province.setTextColor(mContext.getResources().getColor(R.color.region_black));
                tv_city.setTextColor(mContext.getResources().getColor(R.color.theme_red));
                tv_zone_dialog.setTextColor(mContext.getResources().getColor(R.color.region_black));
                tv_area.setTextColor(mContext.getResources().getColor(R.color.region_black));
            }
        });
        tv_zone_dialog.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                lv_city.setVisibility(View.GONE);
                lv_zone.setVisibility(View.VISIBLE);
                lv_province.setVisibility(View.GONE);
                lv_area.setVisibility(View.GONE);
                tv_province.setTextColor(mContext.getResources().getColor(R.color.region_black));
                tv_city.setTextColor(mContext.getResources().getColor(R.color.region_black));
                tv_zone_dialog.setTextColor(mContext.getResources().getColor(R.color.theme_red));
                tv_area.setTextColor(mContext.getResources().getColor(R.color.region_black));
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                dialog.dismiss();
            }
        });
        tv_area.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                lv_city.setVisibility(View.GONE);
                lv_zone.setVisibility(View.GONE);
                lv_province.setVisibility(View.GONE);
                lv_area.setVisibility(View.VISIBLE);
                tv_province.setTextColor(mContext.getResources().getColor(R.color.region_black));
                tv_city.setTextColor(mContext.getResources().getColor(R.color.region_black));
                tv_area.setTextColor(mContext.getResources().getColor(R.color.theme_red));
                tv_zone_dialog.setTextColor(mContext.getResources().getColor(R.color.region_black));
            }
        });

        lv_province.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int position, long l)
            {
                if (mProvinDataListenr != null)
                {
                    cityList = mProvinDataListenr.setOnProvinceSelected(proince.get(position));
                    mContext.runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            if (cityList == null || cityList.size() == 0)
                            {
                                Toast.makeText(mContext, "没有获取到该省份的城市数据", Toast.LENGTH_LONG);
                                return;
                            }
                            tv_province.setText(proince.get(position).getName());
                            tv_province.setEnabled(true);
                            tv_city.setVisibility(View.VISIBLE);
                            tv_province.setTextColor(mContext.getResources().getColor(R.color.region_black));
                            tv_city.setTextColor(mContext.getResources().getColor(R.color.theme_red));
                            tv_zone_dialog.setTextColor(mContext.getResources().getColor(R.color.region_black));
                            tv_area.setTextColor(mContext.getResources().getColor(R.color.region_black));
//                                            setCityAdapter();
                            cityAdapter.replaceAll(cityList);
                            lv_province.setVisibility(View.GONE);
                            lv_zone.setVisibility(View.GONE);
                            lv_area.setVisibility(View.GONE);
                            lv_city.setVisibility(View.VISIBLE);
                            lv_city.setAdapter(cityAdapter);
                        }
                    });
                }
            }
        });
        lv_city.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int position, long l)
            {

                if (mProvinDataListenr != null)
                {
                    zoneList = mProvinDataListenr.setOnCitySelected(cityList.get(position));
//                    mCity=cityList.get(position).getId();
                    mContext.runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            if (zoneList == null || zoneList.size() == 0)
                            {
                                Toast.makeText(mContext, "没有获取到该城市的区域数据", Toast.LENGTH_LONG);
                                return;
                            }
                            zoneAdapter.replaceAll(zoneList);
                            tv_city.setText(cityList.get(position).getName());
                            tv_city.setTextColor(mContext.getResources().getColor(R.color.region_black));
                            tv_province.setTextColor(mContext.getResources().getColor(R.color.region_black));
                            tv_area.setTextColor(mContext.getResources().getColor(R.color.region_black));
                            tv_zone_dialog.setTextColor(mContext.getResources().getColor(R.color.theme_red));
                            tv_zone_dialog.setVisibility(View.VISIBLE);
                            tv_city.setEnabled(true);
                            lv_province.setVisibility(View.GONE);
                            lv_zone.setVisibility(View.VISIBLE);
                            lv_city.setVisibility(View.GONE);
                            lv_area.setVisibility(View.GONE);
                        }
                    });
                }
            }
        });
        lv_zone.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int position, long l)
            {
                if (mProvinDataListenr != null)
                {
                    areaList = mProvinDataListenr.setOnZoneSelected(zoneList.get(position));
                    if (areaList == null || areaList.size() == 0 || !isFour)
                    {
                        if(!isMultiSelect())
                        {
                            dialog.dismiss();
                            return;
                        }
                    }

                    tv_zone_dialog.setText(zoneList.get(position).getName());
                    tv_zone_dialog.setTextColor(mContext.getResources().getColor(R.color.region_black));
                    tv_province.setTextColor(mContext.getResources().getColor(R.color.region_black));
                    tv_area.setTextColor(mContext.getResources().getColor(R.color.region_black));
                    tv_area.setTextColor(mContext.getResources().getColor(R.color.theme_red));
                    if (isFour)
                    {
                        tv_area.setVisibility(View.VISIBLE);
                    }
                    tv_zone_dialog.setEnabled(true);
                    lv_province.setVisibility(View.GONE);
                    lv_zone.setVisibility(View.GONE);
                    lv_city.setVisibility(View.GONE);
                    lv_area.setVisibility(View.VISIBLE);
                    areaAdapter.replaceAll(areaList);
                }
            }
        });
        lv_area.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
            {

                if(!isMultiSelect())
                {
                    dialog.dismiss();
                }
                CheckBox box = view.findViewById(R.id.check);
                int id = areaList.get(i).getId();
                if(checkMap.containsKey(id))
                {
                    checkMap.remove(id);
                    box.setChecked(false);
                }
                else
                {
                    checkMap.put(id, areaList.get(i));
                    box.setChecked(true);
                }
                if (mProvinDataListenr != null)
                {
                    mProvinDataListenr.setOnAreaSelected(areaList.get(i));
                    mProvinDataListenr.updateSelectedList(getSelectList());
                }

                update(getSelectList());
            }
        });
    }

    public void unSelectItem(SelectBean bean)
    {
        if(checkMap.containsKey(bean.getId()))
        {
            checkMap.remove(bean.getId());
        }
        update(getSelectList());
    }

    public void setProvinceData(List<SelectBean> SelectBeans)
    {
        proince = SelectBeans;
        if (proince != null && proince.size() > 0)
        {
            provinceAdapter.replaceAll(proince);
        }
    }

    public Dialog showDialog()
    {
        if (mProvinDataListenr == null)
        {
            return null;
        }

        setProvinceData(mProvinDataListenr.setProvinceList());
        if (proince == null || proince.size() <= 0)
        {
            Toast.makeText(mContext, "请先初始化数据", Toast.LENGTH_LONG);
            return null;
        }
        Window win = dialog.getWindow();
        win.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = mContext.getResources().getDisplayMetrics().heightPixels / 2;
        win.setWindowAnimations(R.style.dialogButtomInStyle);
        win.setAttributes(lp);
        dialog.show();
        return dialog;
    }

    public void setOnRegionDataSetListenr(OnRegionDataSetListener provinceDataSetListenr)
    {
        mProvinDataListenr = provinceDataSetListenr;
    }

    public boolean isMultiSelect()
    {
        return isMultiSelect;
    }

    public void setMultiSelect(boolean multiSelect)
    {
        isMultiSelect = multiSelect;
    }

    public List<SelectBean> getSelectList()
    {
        ArrayList<SelectBean> list = new ArrayList<>();
        list.addAll(checkMap.values());
        return list;
    }

    public void update(List<SelectBean> list )
    {
        flex.removeAllViews();
        LayoutInflater inflater = mContext.getLayoutInflater();
        for (int i = 0; i < list.size(); i++)
        {
            SelectBean bean = list.get(i);
            View view = inflater.inflate(R.layout.layout_tab, null);
            TextView textView = view.findViewById(R.id.text_label);
            textView.setText(bean.getName());

            View click_ = view.findViewById(R.id.text_label_close);
            click_.setTag(bean);
            click_.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    SelectBean bean = (SelectBean) v.getTag();
                    unSelectItem(bean);
                    try
                    {
                        areaAdapter.notifyDataSetChanged();
                    }catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            });
            FlexboxLayout.LayoutParams params = new FlexboxLayout.LayoutParams(FlexboxLayout.LayoutParams.WRAP_CONTENT, FlexboxLayout.LayoutParams.WRAP_CONTENT);
//            params.setFlexBasisPercent(0.2f);//相对父类所占的比例 ,可以控制一行有几个标签
            params.setMargins(20, 10, 0, 0);//根据需求自己设置
            view.setLayoutParams(params);
            flex.addView(view);
        }
    }

}
